package com.tiantian.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.tiantian.cache.user.UserInfoCache;
import com.tiantian.cache.user.UserInfoImpl;
import com.tiantian.domain.BackstageRole;
import com.tiantian.domain.BackstageUser;
import com.tiantian.service.BackstageRoleService;
import com.tiantian.service.BackstageUserService;
import com.tiantian.util.ConfigUtil;
import com.tiantian.util.RoleUtils;
import com.tiantian.web.controller.interceptor.UserInfoInitInterceptor;

@Controller
public class LoginController {

	@Autowired
	private BackstageUserService backstageUserService;
	@Autowired
	private BackstageRoleService backstageRoleService;

	/**
	 * 用户进入登录页面
	 * 
	 * @return
	 */
	@RequestMapping(value = { "/tologin" })
	public String tologin() {
		return "/login";
	}

	@RequestMapping(value = { "/login" })
	public String login(Model model, @RequestParam String username, @RequestParam String password, HttpServletRequest request) {
		BackstageUser user = backstageUserService.findByUsername(username);
		if (user == null) {
			model.addAttribute("message", "用户名不存在");
			return "/login";
		}
		if (StringUtils.equals(password, user.getPassword())) {
			UserInfoImpl userInfo = new UserInfoImpl();
			UserInfoCache.put(userInfo);
			userInfo.setUser(user);

			List<BackstageRole> roles = backstageRoleService.findByUserId(user.getId());
			userInfo.setRoles(roles);

			Map<String, String> menus = new HashMap<String, String>();
			if (roles != null) {
				for (BackstageRole role : roles) {
					menus.putAll(RoleUtils.parseAuthoritys(role.getAuthoritys(), role.getId()));
				}
			}
			userInfo.setMenus(menus);
			userInfo.setIpAddress(getRemoteHost(request));

			userInfo.setRealPath(request.getSession().getServletContext().getRealPath("/"));
			userInfo.setServerPath(request.getSession().getServletContext().getContextPath());

			request.getSession().setAttribute(UserInfoInitInterceptor.SESSION_USERINFO, userInfo);
			request.getSession().setAttribute("pic_root", ConfigUtil.getProperty(ConfigUtil.PIC_ROOT));
			model.addAttribute("message", "登陆成功");
			return "redirect:/index";
		}
		model.addAttribute("message", "密码不对");
		return "/login";
	}

	public String getRemoteHost(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip.equals("0:0:0:0:0:0:0:1") ? "127.0.0.1" : ip;
	}

	/**
	 * 用户退出登录
	 * 
	 * @param session
	 * @return
	 */
	@RequestMapping(value = { "/logout", "/signout" })
	public String logout(HttpSession session) {
		session.invalidate();
		session = null;
		return "redirect:/tologin";
	}

}
